package cli;

import database.DatabaseHelper;
import entity.Course;
import statistic.ScoreDistributionList;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Scanner;

public class ScoreDistribution implements CommandLineInterface.Handler {

    DatabaseHelper db;

    private boolean isSpecificCourse(Scanner in, PrintWriter out) {
        out.println("综合成绩的分布，还是特定课程成绩的分布？");
        String str = in.next();
        return str.contains("课程");
    }

    private Course inputCourse(Scanner in, PrintWriter out) throws SQLException {
        out.println("请输入课程号或课程名：");
        String str = in.next();
        Course cou;
        if (str.matches("[a-zA-Z0-9]*[0-9][a-zA-Z0-9]*")) {
            cou = Course.find(db, str);
        } else {
            ArrayList<Course> courses = Course.findByName(db, str);
            if (courses.isEmpty()) {
                cou = null;
            } else if (courses.size() == 1) {
                cou = courses.get(0);
            } else {
                out.println("有多个课程重名：");
                String[] header = {"课程号", "课程名"};
                String[][] table = new String[courses.size()][header.length];
                for (int i = 0; i < courses.size(); ++i) {
                    table[i][0] = courses.get(i).getId();
                    table[i][1] = courses.get(i).getName();
                }
                out.print(new TableLayout(header, table));
                out.println("请输入课程号：");
                cou = Course.find(db, in.next());
            }
        }
        return cou;
    }

    private void output(PrintWriter out, ScoreDistributionList list) {
        String[] header = {"分数段", "人数", "占比"};
        String[][] table = new String[list.nums.size()][header.length];
        String[] segment = {"[0, 60)", "[60, 70)", "[70, 80)", "[80, 90)", "[90, 100]"};
        for (int i = 0; i < list.nums.size(); ++i) {
            table[i][0] = segment[i];
            table[i][1] = Integer.toString(list.nums.get(i));
            table[i][2] = String.format("%.1f%%", 100 * (double) list.nums.get(i) / list.total);
        }
        out.println(new TableLayout(header, table));
        out.println("总人数：\t" + list.total);
    }

    @Override
    public void handle(Scanner in, PrintWriter out) {
        try {
            db = new DatabaseHelper();
            ScoreDistributionList list;
            if (isSpecificCourse(in, out)) {
                Course cou = inputCourse(in, out);
                if (cou == null) {
                    out.println("课程不存在！");
                    return;
                }
                list = new ScoreDistributionList(cou, new double[] {0, 60, 70, 80, 90, 101});
            } else {
                list = new ScoreDistributionList(new double[] {0, 60, 70, 80, 90, 101});
            }
            output(out, list);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            db.close();
        }
    }
}
